package sg.edu.nus.iss.vmcs.controller;

import java.text.DecimalFormat;


import javax.swing.JOptionPane;
import sg.edu.nus.iss.vmcs.entity.DrinksBrand;
import sg.edu.nus.iss.vmcs.ui.CoinInputBox;
import sg.edu.nus.iss.vmcs.ui.CustomerPanel;
import sg.edu.nus.iss.vmcs.ui.DrinkSelectionBox;
import sg.edu.nus.iss.vmcs.util.VMCSException;

public class TransactionController  {

	private DrinkSelectionBox drinkSelectionBox;
	private CoinInputBox coinInputBox;
	private boolean transactionInprogress = false;
	
	
	
	private MoneyReceiver moneyReceiver;
	
	
	public TransactionController() {
	}
	
	public void startTransaction(int itemId) throws VMCSException{
		
		//DrinkStore drnStr = new DrinkStore(); //TODO Agathiyan - Factory
		//StoreItem item = drnStr.getItem (itemId);
		//StoreObject strObj = item.getContent();
		
		//DrinksBrand brand = CustomerPanel.getBrands().get(itemId+"");
		
		
		//DrinksBrand brand = new DrinksBrand(strObj.getName(), 0.8); //TODO Agathiyan - Factory
		
		
		//set TransactionInProgress		
		
		setTransactionInprogress(true);
		
		
		
		
		
	}
	
//	/**
//     * This method Terminates the transaction when the customer terminates
//	 * @throws VMCSException 
//     * */
//	public void cancelTransaction() throws VMCSException 
//	{
//		if(isTransactionInprogress())
//		{
//			try {
//				moneyReceiver.stopReceive();
//			} catch (VMCSException e) {
//				
//				e.printStackTrace();
//			}
//		}
//		if((isTransactionInprogress()) && getCoinInputBox().getMp().getTotalCoinsInserted()>0){
//			try {
//				moneyReceiver.refundCash();
//			} catch (Exception e1) {
//				e1.printStackTrace();
//			}
//		}
//		//Allow the selection of the brand subsequently by setting true
//		allowDrinkSelectionBox(true);
//		
//	}
//	
//	
//	/**
//     * This method Terminates the transaction when the Maintainer logs in
//	 * @throws VMCSException 
//     * */
//	public void terminateTransaction() throws VMCSException {
//		if(! isTransactionInprogress()){
//			allowDrinkSelectionBox(false);
//			
//			
//		}
//		if((isTransactionInprogress())&& getCoinInputBox().getMp().getTotalCoinsInserted()>0){
//			try {
//				// StopReceiving the coins and refund the total entered.
//				moneyReceiver.stopReceive();
//				moneyReceiver.refundCash();
//				
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		setTransactionInprogress(false);
//		
//	}
//
//	
	

	/**
     * This method will terminate transaction if any error occured after inserted coin
     * @throws VMCSException 
     * */
	public void terminateFault(CoinReceiver coinReceiver) throws VMCSException{
	
		allowCoinInputBoxSelection(false);
		allowDrinkSelectionBox(true);
		
		setTransactionInprogress(false);
		if( getCoinInputBox().getMp().getTotalCoinsInserted() >0){
		
			try {
				
			 coinReceiver.refundCash();
				//moneyReceiver.refundCash();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	/**
     * This method Completes the transaction of the customer.
     * */
	public void completeTransaction(double changeGivenAmt,CoinReceiver coinReceiver) throws VMCSException {
		
		try{
			
			
			
			new ChangeGiver().giveChange(changeGivenAmt);
			
			DecimalFormat twoDForm = new DecimalFormat("#.##");
			//JOptionPane.showMessageDialog(null,"Changed given amt. " + Double.valueOf(twoDForm.format(changeGivenAmt)) + " c ");
			
			//Display Total Inserted after change given
			getCoinInputBox().getMp().setTotalCoinsInserted(0);
			getCoinInputBox().getMp().notifyObservers(0 + " c");
			
			
			//TODO : Activate DrinkSelection Box and Deactivate CoinInputBox
			allowCoinInputBoxSelection(false);
			allowDrinkSelectionBox(true);
			
			//TODO : dispense Drink
			JOptionPane.showMessageDialog(null,"Please collect your drink. Thank you!");
		}catch (Exception e){
			e.printStackTrace();
			terminateFault(coinReceiver);
		}
		
		
		
	}
	
	/**
	 * This method allow CoinInputBox enable or disable
	 * @param flag
	 */
	
	public void allowCoinInputBoxSelection(boolean flag){
		coinInputBox.setCoinInputEnabled(new Boolean(flag));
		coinInputBox.notifyObservers(new Boolean(flag));
		
		
	}	
	
	
	/**
	 * This method allow DrinkSelectionBox enable or disable
	 * @param flag
	 */
	
	public void allowDrinkSelectionBox(boolean flag){
		drinkSelectionBox.setDrinkSelected(new Boolean(flag));
		drinkSelectionBox.notifyObservers(new Boolean(flag));
	
		
	}
	
	public void displayTotalInsertedMoney(double totalInsertedMoney){
		DecimalFormat twoDForm = new DecimalFormat("#.##");
				coinInputBox.getMp().notifyObservers(Double.valueOf(twoDForm.format(totalInsertedMoney)) + " c");
	}
	
	public void displayReutndMoney(double totalInsertedMoney){
		coinInputBox.getMp().setTotalCoinsInserted(0);
		coinInputBox.getMp().notifyObservers(0 + " c");
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		JOptionPane.showMessageDialog(null,"Fault Detected. " + Double.valueOf(twoDForm.format(totalInsertedMoney)) + " c will be refunded.");
				
	}
	
	/**
	 * This method returns the current transaction is whether in progress or not
	 * @return boolean
	 */
	public boolean isTransactionInprogress() {
		return transactionInprogress;
	}
	
	/**
	 * This method set the transaction is whether in progress or not.
	 * @param transactionInprogress
	 */
	public void setTransactionInprogress(boolean transactionInprogress) {
		this.transactionInprogress = transactionInprogress;
	}
	
	/**
	 * This method set current MoneyReceiver
	 * @param MoneyReceiver
	 */
	public void setMoneyReceiver(MoneyReceiver receiver){
		moneyReceiver = receiver;
	}
	public CoinInputBox getCoinInputBox() {
		return coinInputBox;
	}
	public DrinkSelectionBox getDrinkSelectionBox() {
		return drinkSelectionBox;
	}
	public void setDrinkSelectionBox(DrinkSelectionBox drinkSelectionBox) {
		this.drinkSelectionBox = drinkSelectionBox;
	}
	public void setCoinInputBox(CoinInputBox coinInputBox) {
		this.coinInputBox = coinInputBox;
	}
	
	
	
}
